<template>
  <div class="paths-content">
    <el-card class="paths-container" shadow="hover">
      <template #header>
        <div class="card-header">
          <div>
            <h3>学习路径管理</h3>
            <p>创建和管理结构化的学习路径</p>
          </div>
          <el-button type="primary" @click="$emit('open-modal', 'path')" class="create-path-btn">
            <el-icon><Reading /></el-icon>
            <span>创建路径</span>
          </el-button>
        </div>
      </template>

      <div class="paths-list">
        <el-card 
          v-for="path in learningPaths" 
          :key="path.id"
          class="path-card"
          shadow="hover"
        >
          <div class="path-content">
            <div class="path-header">
              <div class="path-icon-wrapper">
                <div class="path-icon">
                  <el-icon><Reading /></el-icon>
                </div>
                <div class="path-progress-ring">
                  <svg viewBox="0 0 36 36" class="circular-chart">
                    <path class="circle-bg"
                      d="M18 2.0845
                        a 15.9155 15.9155 0 0 1 0 31.831
                        a 15.9155 15.9155 0 0 1 0 -31.831"
                    />
                    <path class="circle"
                      :stroke-dasharray="`${path.completion}, 100`"
                      d="M18 2.0845
                        a 15.9155 15.9155 0 0 1 0 31.831
                        a 15.9155 15.9155 0 0 1 0 -31.831"
                    />
                  </svg>
                  <span class="percentage">{{ path.completion }}%</span>
                </div>
              </div>
              
              <div class="path-info">
                <div class="path-title-row">
                  <h3 class="path-title">{{ path.name }}</h3>
                  <div class="path-badges">
                    <el-tag v-if="path.trending" type="warning" effect="dark" class="trending-tag">
                      <el-icon><Lightning /></el-icon>
                      <span>热门</span>
                    </el-tag>
                    <div class="path-rating">
                      <el-icon color="#F59E0B"><Star /></el-icon>
                      <span>{{ path.rating }}</span>
                    </div>
                  </div>
                </div>
                
                <div class="path-tags">
                  <el-tag 
                    :type="getDifficultyTagType(path.difficulty)" 
                    effect="plain"
                    class="difficulty-tag"
                  >
                    {{ getDifficultyLabel(path.difficulty) }}
                  </el-tag>
                  <el-tag 
                    :type="getPathTypeTagType(path.type)" 
                    effect="plain"
                    class="type-tag"
                  >
                    {{ getPathTypeLabel(path.type) }}
                  </el-tag>
                </div>
              </div>
              
              <div class="path-actions">
                <el-button 
                  circle 
                  @click="togglePathExpansion(path.id)"
                  :icon="expandedPaths.has(path.id) ? ArrowDown : ArrowRight"
                  class="expand-btn"
                />
                <el-button circle :icon="Edit" @click="editPath(path)" class="edit-btn" />
                <el-button circle type="danger" :icon="Delete" @click="deletePath(path)" class="delete-btn" />
              </div>
            </div>
            
            <div class="path-stats">
              <div class="stat-item nodes-stat">
                <div class="stat-icon">
                  <el-icon><Collection /></el-icon>
                </div>
                <div class="stat-text">
                  <span class="stat-label">知识节点</span>
                  <span class="stat-value">{{ path.nodeCount }}</span>
                </div>
              </div>
              <div class="stat-item time-stat">
                <div class="stat-icon">
                  <el-icon><Clock /></el-icon>
                </div>
                <div class="stat-text">
                  <span class="stat-label">预计时长</span>
                  <span class="stat-value">{{ path.estimatedTime }}</span>
                </div>
              </div>
              <div class="stat-item learners-stat">
                <div class="stat-icon">
                  <el-icon><User /></el-icon>
                </div>
                <div class="stat-text">
                  <span class="stat-label">学习者</span>
                  <span class="stat-value">{{ path.learners }}</span>
                </div>
              </div>
              <div class="stat-item completion-stat">
                <div class="stat-icon">
                  <el-icon><DataAnalysis /></el-icon>
                </div>
                <div class="stat-text">
                  <span class="stat-label">完成率</span>
                  <span class="stat-value">{{ path.completion }}%</span>
                </div>
              </div>
            </div>

            <!-- 展开的节点序列 -->
            <el-collapse-transition>
              <div v-if="expandedPaths.has(path.id)" class="path-nodes">
                <div class="nodes-header">
                  <div class="nodes-title">
                    <el-icon><Share /></el-icon>
                    <span>学习路径节点</span>
                  </div>
                  <div class="nodes-count">
                    共 {{ path.nodes.length }} 个节点
                  </div>
                </div>
                
                <div class="nodes-timeline">
                  <div 
                    v-for="(node, index) in path.nodes" 
                    :key="index" 
                    class="timeline-item"
                    :class="{ 'completed': index < Math.floor(path.nodes.length * path.completion / 100) }"
                  >
                    <div class="timeline-connector" v-if="index < path.nodes.length - 1"></div>
                    <div class="timeline-node">
                      <div class="node-step">{{ index + 1 }}</div>
                      <div class="node-content">
                        <div class="node-name">{{ node }}</div>
                        <div class="node-status">
                          <span v-if="index < Math.floor(path.nodes.length * path.completion / 100)" class="status-completed">已完成</span>
                          <span v-else-if="index === Math.floor(path.nodes.length * path.completion / 100)" class="status-current">学习中</span>
                          <span v-else class="status-pending">待学习</span>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </el-collapse-transition>
          </div>
        </el-card>
      </div>

      <!-- 空状态 -->
      <el-empty v-if="learningPaths.length === 0" description="暂无学习路径">
        <el-button type="primary" @click="$emit('open-modal', 'path')">
          创建第一个学习路径
        </el-button>
      </el-empty>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { 
  Reading, Lightning, Star, ArrowDown, ArrowRight, Edit, Delete, 
  Collection, Clock, User, DataAnalysis, Share 
} from '@element-plus/icons-vue'

// Props
defineProps({
  learningPaths: {
    type: Array,
    default: () => []
  },
  allNodes: {
    type: Array,
    default: () => []
  }
})

// Emits
defineEmits(['open-modal'])

// 响应式数据
const expandedPaths = ref(new Set())

// 示例学习路径数据
const learningPaths = ref([
  {
    id: 1,
    name: '前端开发完整路径',
    category: 'frontend',
    type: 'system',
    difficulty: 'beginner',
    nodeCount: 8,
    estimatedTime: '4-6个月',
    learners: 189,
    completion: 78,
    rating: 4.8,
    trending: true,
    nodes: ['HTML结构', 'CSS样式', 'JavaScript编程', 'React框架', 'Vue.js框架', 'Node.js基础', '项目实战', '部署上线']
  },
  {
    id: 2,
    name: '现代前端框架精通',
    category: 'frontend',
    type: 'template',
    difficulty: 'advanced',
    nodeCount: 12,
    estimatedTime: '6-8个月',
    learners: 67,
    completion: 85,
    rating: 4.9,
    trending: false,
    nodes: ['JavaScript编程', 'React框架', 'Vue.js框架', 'TypeScript', 'Webpack配置', 'Vue3 Composition API', 'React Hooks', '状态管理', '性能优化', '单元测试', '项目架构', '最佳实践']
  },
  {
    id: 3,
    name: '全栈开发进阶',
    category: 'full-stack',
    type: 'custom',
    difficulty: 'intermediate',
    nodeCount: 15,
    estimatedTime: '8-12个月',
    learners: 134,
    completion: 65,
    rating: 4.6,
    trending: true,
    nodes: ['React框架', 'Node.js', '数据库设计', 'API开发', 'Express框架', 'MongoDB', 'RESTful API', 'GraphQL', '身份认证', '部署运维', 'Docker容器', 'CI/CD', '监控告警', '性能调优', '项目管理']
  }
])

// 方法
const togglePathExpansion = (pathId) => {
  const newExpanded = new Set(expandedPaths.value)
  if (newExpanded.has(pathId)) {
    newExpanded.delete(pathId)
  } else {
    newExpanded.add(pathId)
  }
  expandedPaths.value = newExpanded
}

const getDifficultyTagType = (difficulty) => {
  const types = {
    beginner: 'success',
    intermediate: 'warning',
    advanced: 'danger'
  }
  return types[difficulty] || 'info'
}

const getDifficultyLabel = (difficulty) => {
  const labels = {
    beginner: '🌱 初级',
    intermediate: '🌿 中级',
    advanced: '🌳 高级'
  }
  return labels[difficulty] || '🌿 中级'
}

const getPathTypeTagType = (type) => {
  const types = {
    system: 'primary',
    template: '',
    custom: 'info'
  }
  return types[type] || 'info'
}

const getPathTypeLabel = (type) => {
  const labels = {
    system: '系统路径',
    template: '模板路径',
    custom: '自定义'
  }
  return labels[type] || '未知类型'
}

const editPath = (path) => {
  ElMessage.info(`编辑路径: ${path.name}`)
}

const deletePath = (path) => {
  ElMessageBox.confirm(
    `确定要删除 "${path.name}" 学习路径吗？`,
    '删除确认',
    {
      confirmButtonText: '确定删除',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    ElMessage.success('学习路径删除成功')
  })
}
</script>

<style scoped>
.paths-content {
  width: 100%;
}

.paths-container {
  background: white;
  border-radius: 16px;
  border: none;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  overflow: hidden;
}

.paths-container :deep(.el-card__header) {
  background: linear-gradient(135deg, #faf8fc 0%, #f8fafc 100%);
  border-bottom: 1px solid #f1f5f9;
  padding: 24px 28px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header h3 {
  font-size: 20px;
  font-weight: 700;
  color: #1e293b;
  margin: 0 0 4px 0;
}

.card-header p {
  color: #6b7280;
  margin: 0;
  font-size: 14px;
}

.create-path-btn {
  background: linear-gradient(135deg, #9f8eaf 0%, #c5a7be 100%);
  border: none;
  border-radius: 12px;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(159, 142, 175, 0.25);
}

.create-path-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(159, 142, 175, 0.35);
}

.paths-list {
  display: flex;
  flex-direction: column;
  gap: 24px;
  padding: 8px;
}

.path-card {
  border-radius: 16px;
  border: 1px solid #f1f5f9;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  background: white;
  overflow: hidden;
}

.path-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 16px 32px rgba(159, 142, 175, 0.12);
  border-color: #e8cadb;
}

.path-content {
  padding: 28px;
}

.path-header {
  display: flex;
  align-items: flex-start;
  gap: 20px;
  margin-bottom: 24px;
}

.path-icon-wrapper {
  position: relative;
  flex-shrink: 0;
}

.path-icon {
  width: 56px;
  height: 56px;
  background: linear-gradient(135deg, #9f8eaf 0%, #c5a7be 100%);
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 24px;
  box-shadow: 0 8px 24px rgba(159, 142, 175, 0.3);
}

.path-progress-ring {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 32px;
  height: 32px;
}

.circular-chart {
  width: 32px;
  height: 32px;
  transform: rotate(-90deg);
}

.circle-bg {
  fill: none;
  stroke: #f1f5f9;
  stroke-width: 3;
}

.circle {
  fill: none;
  stroke: #9f8eaf;
  stroke-width: 3;
  stroke-linecap: round;
}

.percentage {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 10px;
  font-weight: 700;
  color: #9f8eaf;
}

.path-info {
  flex: 1;
}

.path-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
  flex-wrap: wrap;
  gap: 12px;
}

.path-title {
  font-size: 20px;
  font-weight: 700;
  color: #1e293b;
  margin: 0;
  line-height: 1.3;
}

.path-badges {
  display: flex;
  align-items: center;
  gap: 12px;
}

.trending-tag {
  display: flex;
  align-items: center;
  gap: 4px;
  font-weight: 600;
  font-size: 12px;
}

.path-rating {
  display: flex;
  align-items: center;
  gap: 4px;
  color: #f59e0b;
  font-weight: 700;
  background: #fef3c7;
  padding: 4px 8px;
  border-radius: 8px;
  font-size: 13px;
}

.path-tags {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.difficulty-tag,
.type-tag {
  font-weight: 600;
  border-radius: 8px;
  font-size: 12px;
}

.path-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
  opacity: 0;
  transition: all 0.3s ease;
}

.path-card:hover .path-actions {
  opacity: 1;
}

.expand-btn,
.edit-btn,
.delete-btn {
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid #f1f5f9;
  backdrop-filter: blur(10px);
}

.expand-btn:hover,
.edit-btn:hover {
  background: #9f8eaf;
  border-color: #9f8eaf;
  color: white;
  transform: scale(1.1);
}

.path-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px;
  border-radius: 12px;
  transition: all 0.3s ease;
}

.nodes-stat { background: linear-gradient(135deg, #e8cadb 0%, #faf1f8 100%); }
.time-stat { background: linear-gradient(135deg, #d2dee5 0%, #f8fafc 100%); }
.learners-stat { background: linear-gradient(135deg, #f5e2c3 0%, #fdf4e3 100%); }
.completion-stat { background: linear-gradient(135deg, #c9afbc 0%, #f8fafc 100%); }

.stat-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.7);
  color: #9f8eaf;
  flex-shrink: 0;
}

.stat-text {
  flex: 1;
}

.stat-label {
  display: block;
  font-size: 12px;
  color: #64748b;
  margin-bottom: 4px;
  font-weight: 500;
}

.stat-value {
  display: block;
  font-size: 16px;
  font-weight: 700;
  color: #1e293b;
}

.path-nodes {
  padding: 24px;
  background: linear-gradient(135deg, #faf8fc 0%, #f8fafc 100%);
  border-radius: 16px;
  border: 1px solid #f1f5f9;
}

.nodes-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.nodes-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
  font-weight: 700;
  color: #1e293b;
}

.nodes-count {
  font-size: 13px;
  color: #64748b;
  background: white;
  padding: 6px 12px;
  border-radius: 20px;
  font-weight: 600;
}

.nodes-timeline {
  position: relative;
}

.timeline-item {
  position: relative;
  margin-bottom: 20px;
}

.timeline-item:last-child {
  margin-bottom: 0;
}

.timeline-connector {
  position: absolute;
  left: 18px;
  top: 36px;
  width: 2px;
  height: 32px;
  background: #e5e7eb;
}

.timeline-item.completed .timeline-connector {
  background: #9f8eaf;
}

.timeline-node {
  display: flex;
  align-items: center;
  gap: 16px;
}

.node-step {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 14px;
  border: 3px solid #e5e7eb;
  background: white;
  color: #64748b;
  flex-shrink: 0;
}

.timeline-item.completed .node-step {
  background: #9f8eaf;
  border-color: #9f8eaf;
  color: white;
}

.node-content {
  flex: 1;
  background: white;
  padding: 12px 16px;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  border: 1px solid #f1f5f9;
}

.timeline-item.completed .node-content {
  border-color: #e8cadb;
  background: linear-gradient(135deg, #faf8fc 0%, white 100%);
}

.node-name {
  font-size: 15px;
  font-weight: 600;
  color: #1e293b;
  margin-bottom: 4px;
}

.node-status {
  font-size: 12px;
  font-weight: 600;
}

.status-completed {
  color: #059669;
  background: #d1fae5;
  padding: 2px 8px;
  border-radius: 12px;
}

.status-current {
  color: #d97706;
  background: #fed7aa;
  padding: 2px 8px;
  border-radius: 12px;
}

.status-pending {
  color: #64748b;
  background: #f1f5f9;
  padding: 2px 8px;
  border-radius: 12px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .path-header {
    flex-direction: column;
    align-items: center;
    text-align: center;
    gap: 16px;
  }

  .path-title-row {
    flex-direction: column;
    align-items: center;
    gap: 8px;
  }

  .path-stats {
    grid-template-columns: 1fr;
    gap: 12px;
  }
  
  .nodes-timeline {
    padding: 0 8px;
  }
  
  .timeline-node {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
  
  .timeline-connector {
    left: 17px;
    top: 44px;
    height: 40px;
  }

  .path-actions {
    opacity: 1;
    justify-content: center;
    width: 100%;
  }
}
</style>